import util from '@/network/common/util.js';
import $ from 'jquery'

let postCfg = {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    transformRequest: function (data) {
        return $.param(data)
    }
}
let api = {
    'getAllCategories'       : '/product/v1/getAllCategories', //获取商品分类列表
    'getAllProducts'         : '/product/v1/getAllProducts', //获取商品分类列表
    'putProOrder'            : '/order/v1/putProOrder', //下单
    'orderList'              : '/order/v1/orderList', //获取订单列表
    'deliverComplete'        : '/order/v1/deliverComplete', //订单完成配送
    'aliScanAuthCode2Store'  : '/pay/v1/aliScanAuthCode2Store', //支付宝当面付
    'aliScanQrcode2Store'    : '/pay/v1/aliScanQrcode2Store', //支付宝扫码支付
    
}
let res = {};
function getAllCategories () {
    let p = new Promise(function (resolve, reject) {
        util.ajax.post(api.getAllCategories, {}, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}
function getAllProducts (params) {
    let p = new Promise(function (resolve, reject) {
        util.ajax.post(api.getAllProducts, {
            page: params.page,
            pageSize: params.pageSize,
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}
function putProOrder (params) {
    let p = new Promise(function (resolve, reject) {
        util.ajax.post(api.putProOrder, {
            pros: JSON.stringify(params.pros),
            actualMoeny: params.actualMoeny,
            payType: params.payType,
            isHand: params.isHand,
            roomId: params.roomId,
            isAuth: params.isAuth,
            authMoney: params.authMoney,
            authAccId: params.authAccId,
            vipCardId: params.vipCardId,
            isDeliver: params.isDeliver,
            deviceMac: params.deviceMac,
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}
function orderList (params) {
    let p = new Promise(function (resolve, reject) {
        util.ajax.post(api.orderList, {
            page: params.page,
            pageSize: params.pageSize,
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}
function deliverComplete (oid) {
    let p = new Promise(function (resolve, reject) {
        util.ajax.post(api.deliverComplete, {
            oid: oid
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}
function aliScanAuthCode2Store (params) {
    let p = new Promise(function (resolve, reject) {
        util.ajax.post(api.aliScanAuthCode2Store, {
            outTradeNo: params.outTradeNo,
            authCode: params.authCode,
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}
function aliScanQrcode2Store (outTradeNo) {
    let p = new Promise(function (resolve, reject) {
        util.ajax.post(api.aliScanQrcode2Store, {
            outTradeNo: outTradeNo
        }, postCfg).then(function(res) {
            resolve(res);
        }, function(error) {
            reject(error);
        });
    });
    return p;
}


export default {
    getAllCategories,
    getAllProducts,
    putProOrder,
    orderList,
    deliverComplete,
    aliScanAuthCode2Store,
    aliScanQrcode2Store

}